﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace Algorithms
{
    class Program
    {
        static void Main(string[] args)
        {
            List<int> list = new List<int>() { 3, 1, 3, 5, 5, 5, 9, 1, 1, 7, 7, 7, 7, 2, 5, 5, 5 };
            Console.WriteLine(GetMaxFrequencyNumber(list));

            Console.WriteLine("\r\nPress any key to exit...");
            Console.Read();
        }

        static int? GetMaxFrequencyNumber(IEnumerable<int> numbers)
        {
            int maxFreq = 0;
            int? num = null;
            IDictionary<int, int> map = new Dictionary<int, int>();

            foreach (int n in numbers)
            {
                int freq;
                if (map.TryGetValue(n, out freq))
                {
                    map[n] = ++freq;
                }
                else
                {
                    freq = 1;
                    map.Add(n, freq);
                }

                if (freq > maxFreq)
                {
                    maxFreq = freq;
                    num = n;
                }
            }

            return num.HasValue ? num : null;
        }

        /// <summary>
        /// Convert Integer to Roman String
        /// </summary>
        /// <param name="number"></param>
        /// <returns></returns>
        public static string IntToRoman(int number)
        {
            if (-9999 >= number || number >= 9999)
            {
                throw new ArgumentOutOfRangeException("number");
            }

            if (number == 0)
            {
                return "NULL";
            }

            StringBuilder sb = new StringBuilder(10);

            if (number < 0)
            {
                sb.Append('-');
                number *= -1;
            }

            string[,] table = new string[,] { 
                { "", "I", "II", "III", "IV", "V", "VI", "VII", "VIII", "IX" }, 
                { "", "X", "XX", "XXX", "XL", "L", "LX", "LXX", "LXXX", "XC" }, 
                { "", "C", "CC", "CCC", "CD", "D", "DC", "DCC", "DCCC", "CM" },
                { "", "M", "MM", "MMM", "M(V)", "(V)", "(V)M", "(V)MM", "(V)MMM", "M(X)" } 
            };

            for (int i = 1000, j = 3; i > 0; i /= 10, j--)
            {
                int digit = number / i;
                sb.Append(table[j, digit]);
                number -= digit * i;
            }

            return sb.ToString();
        }
    }
}
